<?php
namespace Chat\Service;

use Zend\EventManager\EventManagerInterface;
use Zend\EventManager\ListenerAggregateInterface;
use Zend\View\Model\ViewModel;
use Zend\View\View;

class ChatListener implements ListenerAggregateInterface
{
	protected $listeners = array();
	
	
	
	public function attach(EventManagerInterface $events){
		$sharedEvents = $events->getSharedManager();
		$this->listeners[] = $sharedEvents->attach('Chat\Service\ChatService','sendChatMessage',
				array($this,'onSendChatMessage'),100);
	}
	
	public function detach(EventManagerInterface $events){
		
		foreach($this->listeners as $index => $listeners){
			if($events->detach($listener)){
				unset($this->listeners[$index]);
			}
		}
	}
	
	public function onSendChatMessage($e){
		$e->getTarget();
		//Update the the view with the latest entry for that user in the db
		//$this->view->headScript()->appendScript("jQuery(function(){alert('$message');});");
		//This requires that you add echo $this->headScript() to your view- or layout-script
		//$view = new Zend\View();
		//$scripts = $view->inlineScript();
		//$scripts->appendScript('alert("new Message")');
		//$e->view->headScript()->appendScript("jQuery(function(){alert('eter');});");
		//$viewRenderer = new Zend\Controller\ActionHelper\ViewRenderer();
		//$viewRenderer->setView($view);
		//var_dump($e->getTarget());
		//return new ViewModel();
	}
}